#include "../core/engine/world.h"
#include "node.h"

namespace sleek
{
    namespace node
    {
        Node::Node(core::engine::engine *m) : smgr(m), reference()
        {
            setScale(core::math::vector3df(1,1,1));
            body = 0;
        }
        Node::~Node()
        {
            if(body) delete body;
        }
        void Node::enablePhysics(physics::body_geometry ge, physics::body_type tp)
        {
            if(body) disablePhysics();
            body = new physics::Physics(smgr);
            body->setPosition(pos);
            body->setUserData(this);
            body->setType(tp);
        }
        void Node::disablePhysics()
        {
            if(body) delete body;
            body = 0;
        }
        physics::Physics* Node::getPhysics()
        {
            return body;
        }
        void Node::setMesh(mesh::MeshBuffer *i)
        {
            mesh = i;
            if(body) body->setGeometry(i);
        }
        void Node::setMaterial(mesh::material *i)
        {
            mat = *i;
        }
        void Node::setPosition(core::math::vector3df i)
        {
            pos = i;
            if(body) body->setPosition(i);
        }
        void Node::setRotation(core::math::vector3df i)
        {
            rot = i;
            if(body) body->setRotation(i);
        }
        void Node::setScale(core::math::vector3df i)
        {
            sca = i;
        }
        core::math::vector3df Node::getScale()
        {
            return sca;
        }
        core::math::vector3df Node::getPosition()
        {
            return pos;
        }
        core::math::vector3df Node::getRotation()
        {
            return rot;
        }
        mesh::material* Node::getMaterial()
        {
            return &mat;
        }
        mesh::MeshBuffer* Node::getMesh()
        {
            return mesh;
        }
        void Node::render()
        {
            smgr->getDrawManager()->setActiveMaterial(&mat);
            if(body)
            {
                pos = body->getPosition();
                rot = body->getRotation();
            }
        }
    }
}
